// xc_lshop/shop/user/barter/code.js
var actmember = require('../../../../xc/model/member')
var actbill = require('../../../../xc/model/poster')

Page({
  mixins: [require('../../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    price: ""
  },
  xcloaddata: function (options, config) {
    console.log(config.yihuo)
    this.setData({
      yihuoconfig: config.yihuo
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000000'
    })
  },
  afterlogin: function (options, config, userinfo) {

    if (userinfo.member_id == 0) { //未登录
      this.login()
    } else {
      this.setData({
        member: userinfo
      })
      this.getinfor()
    }
  },
  set(e) {
    console.log("e")
    console.log(e)
    this.setData({
      price: e.detail.amount
    })
    this.getinfor()
  },
  getinfor() {
    var that = this;
    console.log("this.data.price")
    console.log(this.data.price)

    actmember.getyihuoqrcode(this.data.price).then(function (res) {
      if (res["status"] == 1) {
        var updatemodel = {};
        updatemodel['qrcode'] = res['url'];

        that.setData(updatemodel);

        // that.aftergetuserinfo(that.data.member)

      } else {
        wx.showToast({
          icon: "error",
          title: '获取失败',
        })
      }
    })
  },
  loadposter(posterid) {
    var para = {
      op: "collectioncode"
    };
    para["posterid"] = posterid;
    para["price"] = this.data.price;
    var that = this;
    actbill.poster(para).then(function (res) {
      console.log(res)
      if (res.status == 1) {
        var url = res.obj.url
        that.saveImageToPhotosAlbum(url)
      } else {
        wx.hideLoading()
        wx.showToast({
          title: '保存失败',
        })
      }


    })
  },
  saveImage() {
    wx.showLoading({
      title: '保存中',
    })
    var yihuoconfig = this.data.yihuoconfig;
    if (yihuoconfig.poster && yihuoconfig.poster.status == 1) {
      this.loadposter(yihuoconfig.poster.id)
    } else {
      var qrcode = this.data.qrcode;
      this.saveImageToPhotosAlbum(qrcode)
    }
  },
  //保存
  saveImageToPhotosAlbum(qrcode) {
    console.log("qrcode")

    console.log(qrcode)
    var that = this;
    wx.downloadFile({
      url: qrcode,
      success: function (res) {
        
        var tempFilePath = res.tempFilePath;
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success: function (res) {
          
            wx.hideLoading()
            wx.saveImageToPhotosAlbum({
              filePath: tempFilePath,
              success: function (res) {

                wx.showToast({
                  title: '操作成功',
                })
              },
              fail: function (res) {
                if (res.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                  wx.openSetting({
                    success(settingdata) {}
                  })
                }
              }
            })
          },
          fail(res) {
            wx.hideLoading()
            wx.showModal({
              title: '错误',
              content: '点击右上角，手动开启保存相册权限',
              complete: (res) => {
                if (res.cancel) {
                  
                }
            
                if (res.confirm) {
                  
                }
              }
            })
          }
        })


      },
      fail() {
        console.log("aaaawwwwwwwwwwww")

        wx.hideLoading()
        wx.showToast({
          title: '保存失败',
        })
      }
    })

  },
  opedu() {
    if (this.data.price) {
      this.setData({
        price: ""
      })
      this.getinfor()
    } else {
      this.selectComponent("#setnum").init()
    }

  },
  // 登录注册
  login() {
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
      that.getinfor()
    })
  },

})